Docker安装docker-Compose并配置nginx

1 安装

目前有两种主流安装方式,笔者使用了第一种方式。

第一种
下载最新的docker-compose文件

下载完成后需要对/usr/local/bin/docker-compose目录进行赋权

1
curl -L https://github.com/docker/compose/releases/download/1.8.0/run.sh > /usr/local/bin/docker-compose
1
chmod +x /usr/local/bin/docker-compose

测试结果

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost bin]# docker-compose --version
Unable to find image 'docker/compose:1.8.0' locally
1.8.0: Pulling from docker/compose
e110a4a17941: Pull complete
92120570534d: Pull complete
47d26c525b40: Pull complete
40a1d6f501ac: Pull complete
643031e197d8: Pull complete
0841ec069338: Pull complete
Digest: sha256:9bb1d2f141b4511b52dac37e5ea0aecadaf7786bc47184c133c566a4f678061d
Status: Downloaded newer image for docker/compose:1.8.0
docker-compose version 1.8.0, build f3628c7

卸载

1
rm /usr/local/bin/docker-compose

2 docker-compose 配置挂载 Volume 并运行 nginx

1.先在宿主机创建挂载目录:

1
mkdir -p /data/nginx2/{conf,conf.d,html,logs}

2.然后把nginx的配置文件 nginx.conf 文件放到 /data/nginx2/conf/

nginx.conf配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# nginx.conf配置文件如下
user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

3.将使用的nginx子配置,配置文件 default.conf 放到 /data/nginx2/conf.d/

default.conf 配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# default.conf 配置文件如下 
server {
listen 80;
server_name localhost;

# rewrite ^(.*)$ https://www.vhxsl.com permanent;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

4.docker-compose 的配置nginx 的启动文件conpose-nginx.yml 放到/data/nginx2/

docker-compose.yml 配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#docker-compose.yml 配置文件如下 
version: "3"
services:
composeNginx:
image: nginx
ports:
- "8085:80"
volumes:
- /data/nginx2/html:/usr/share/nginx/html
- /data/nginx2/conf/nginx.conf:/etc/nginx/nginx.conf
- /data/nginx2/conf.d:/etc/nginx/conf.d
- /data/nginx2/logs:/var/log/nginx
restart: always
container_name: composeNginx

5.将一个 index.html 放到 /data/nginx2/html

index.html随便写都可以

1
aaaaaa

6.使用docker-compose 启动nginx

因为我们的yml文件不是docker-compose.yml,在进行服务排编是需要指定yml文件名称

1
docker-compose -f conpose-nginx.yml up -d

效果如下

1
2
3
4
5
6
7
8
9
10
11
12
[wjk@localhost nginx2]# docker-compose -f conpose-nginx.yml up -d
Creating network "root_default" with the default driver
Pulling composeNginx (nginx:latest)...
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
Creating composeNginx ... done

查看服务

1
2
3
[wjk@localhost nginx2]# docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78eb25c4b591 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:8085->80/tcp composeNginx

在外部浏览器可以访问 http://ip:8085/

若报错:

1
2
3
[root@localhost nginx]# docker-compose -f conpose-nginx.yml up -d
ERROR: Version in "./conpose-nginx.yml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a version of "2" (or "2.0") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

linux 版本过于旧了,需要在docker-compose.yml,修改verison值,之前是3.5,现改成2.0即可

继开 wechat
欢迎加我的微信,共同交流技术